﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LoginModule.Models;
using LogModule.Interface;
using MonitorModule.DbContexts;
using MonitorModule.Models;
using Prism.Dialogs;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;
using ProductMonitor.DbContexts;
using StaffModule.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Documents;

namespace ProductMonitor.ViewModels {
    public partial class MainWindowViewModel : ObservableObject {

        #region 字段/属性

        private readonly ProductMonitorDbContext _productMonitorDbContext;

        private readonly ILogService _logger;

        // 区域导航记录
        private IRegionNavigationJournal? _journal;

        // 区域管理:依赖注入取出
        private readonly IRegionManager _regionManager;

        // 对话框服务
        private readonly IDialogService _dialogService;
        // 模块集合
        [ObservableProperty]
        private List<ModuleDetail>? _modules=new() ;


        #endregion

        #region 视图模型构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="RegionManager"></param>
        /// <param name="DialogService"></param>
        public MainWindowViewModel(ILogService logger, IRegionManager RegionManager, IDialogService DialogService, ProductMonitorDbContext productMonitorDbContext) {
            #region 注入
            _productMonitorDbContext = productMonitorDbContext;
            //日志
            _logger = logger;
            _logger.Info("项目主模块初始化");
            //区域管理
            _regionManager = RegionManager;
            //对话框服务,在构造函数实例化
            _dialogService = DialogService;
            #endregion

            #region 初始化
            InitData();
            #endregion


        }
        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        private void InitData() {
            Modules = _productMonitorDbContext.ModuleDetail?.ToList();//模块集合
        }


        #region 命令-展示模块
        [RelayCommand]
        public void ShowModule(string moduleName = "MonitorUC") {
            try {
                if (Modules != null) {
                    foreach (var item in Modules) {
                        if (item.ModuleName == moduleName) {
                            item.IsSelected = true;
                            break;
                        }
                    }
                    _regionManager.Regions["ContentRegion"].RequestNavigate(moduleName);
                    _logger.Info($"跳转到{moduleName}模块");
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                _logger.Error($"跳转到{moduleName}模块失败: ",e);
            }

        }
        #endregion

        #region 设置默认模块
        /// <summary>
        /// 设置默认模块
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="accountInfoModel"></param>
        public void SetDefaultNav(string moduleName, AccountInfoModel accountInfoModel) {
            try {
                if (Modules != null) {
                    foreach (var item in Modules) {
                        if (item.ModuleName == moduleName) {
                            item.IsSelected = true;
                            break;
                        }
                    }
                    NavigationParameters paras = new NavigationParameters();
                    paras.Add("AccountInfoModel", accountInfoModel);
                    _regionManager.Regions["ContentRegion"].RequestNavigate(moduleName, paras);
                    _logger.Info($"跳转到{moduleName}模块");
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                _logger.Error($"跳转到{moduleName}模块失败: ", e);
            }

        }
        #endregion

    }
}
